home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2005 March / Macworld CD March 2005 - Marathon Trilogy.iso / Shareware World / iPod / iPodderX.sit / iPodderX / iPodderX.app / Contents / Resources / btformats.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2005-01-07  |  4.1 KB  |  138 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  3.  
  4. from types import StringType, LongType, IntType, ListType, DictType
  5. from re import compile
  6. reg = compile('^[^/\\\\.~][^/\\\\]*$')
  7. ints = (LongType, IntType)
  8.  
  9. def check_info(info):
  10.     if type(info) != DictType:
  11.         raise ValueError, 'bad metainfo - not a dictionary'
  12.     
  13.     pieces = info.get('pieces')
  14.     if type(pieces) != StringType or len(pieces) % 20 != 0:
  15.         raise ValueError, 'bad metainfo - bad pieces key'
  16.     
  17.     piecelength = info.get('piece length')
  18.     if type(piecelength) not in ints or piecelength <= 0:
  19.         raise ValueError, 'bad metainfo - illegal piece length'
  20.     
  21.     name = info.get('name')
  22.     if type(name) != StringType:
  23.         raise ValueError, 'bad metainfo - bad name'
  24.     
  25.     if not reg.match(name):
  26.         raise ValueError, 'name %s disallowed for security reasons' % name
  27.     
  28.     if info.has_key('files') == info.has_key('length'):
  29.         raise ValueError, 'single/multiple file mix'
  30.     
  31.     if info.has_key('length'):
  32.         length = info.get('length')
  33.         if type(length) not in ints or length < 0:
  34.             raise ValueError, 'bad metainfo - bad length'
  35.         
  36.     else:
  37.         files = info.get('files')
  38.         if type(files) != ListType:
  39.             raise ValueError
  40.         
  41.         for f in files:
  42.             if type(f) != DictType:
  43.                 raise ValueError, 'bad metainfo - bad file value'
  44.             
  45.             length = f.get('length')
  46.             if type(length) not in ints or length < 0:
  47.                 raise ValueError, 'bad metainfo - bad length'
  48.             
  49.             path = f.get('path')
  50.             if type(path) != ListType or path == []:
  51.                 raise ValueError, 'bad metainfo - bad path'
  52.             
  53.             for p in path:
  54.                 if type(p) != StringType:
  55.                     raise ValueError, 'bad metainfo - bad path dir'
  56.                 
  57.                 if not reg.match(p):
  58.                     raise ValueError, 'path %s disallowed for security reasons' % p
  59.                     continue
  60.             
  61.         
  62.         for i in xrange(len(files)):
  63.             for j in xrange(i):
  64.                 if files[i]['path'] == files[j]['path']:
  65.                     raise ValueError, 'bad metainfo - duplicate path'
  66.                     continue
  67.             
  68.         
  69.  
  70.  
  71. def check_message(message):
  72.     if type(message) != DictType:
  73.         raise ValueError
  74.     
  75.     check_info(message.get('info'))
  76.     if type(message.get('announce')) != StringType:
  77.         raise ValueError
  78.     
  79.  
  80.  
  81. def check_peers(message):
  82.     if type(message) != DictType:
  83.         raise ValueError
  84.     
  85.     if message.has_key('failure reason'):
  86.         if type(message['failure reason']) != StringType:
  87.             raise ValueError
  88.         
  89.         return None
  90.     
  91.     peers = message.get('peers')
  92.     if type(peers) == ListType:
  93.         for p in peers:
  94.             if type(p) != DictType:
  95.                 raise ValueError
  96.             
  97.             if type(p.get('ip')) != StringType:
  98.                 raise ValueError
  99.             
  100.             port = p.get('port')
  101.             if type(port) not in ints or p <= 0:
  102.                 raise ValueError
  103.             
  104.             if p.has_key('peer id'):
  105.                 id = p.get('peer id')
  106.                 if type(id) != StringType or len(id) != 20:
  107.                     raise ValueError
  108.                 
  109.             len(id) != 20
  110.         
  111.     elif type(peers) != StringType or len(peers) % 6 != 0:
  112.         raise ValueError
  113.     
  114.     interval = message.get('interval', 1)
  115.     if type(interval) not in ints or interval <= 0:
  116.         raise ValueError
  117.     
  118.     minint = message.get('min interval', 1)
  119.     if type(minint) not in ints or minint <= 0:
  120.         raise ValueError
  121.     
  122.     if type(message.get('tracker id', '')) != StringType:
  123.         raise ValueError
  124.     
  125.     npeers = message.get('num peers', 0)
  126.     if type(npeers) not in ints or npeers < 0:
  127.         raise ValueError
  128.     
  129.     dpeers = message.get('done peers', 0)
  130.     if type(dpeers) not in ints or dpeers < 0:
  131.         raise ValueError
  132.     
  133.     last = message.get('last', 0)
  134.     if type(last) not in ints or last < 0:
  135.         raise ValueError
  136.     
  137.  
  138.